﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GolfLeague.EditModels;
using GolfLeague.Models;
using GolfLeague.ViewModels;

namespace GolfLeague.Controllers
{
    public class ScoringMethodController : Controller
    {
        ScoreInterface scoreInterface = new ScoreInterface();

        //
        // GET: /ScoringMethod/
        
        [Authorize(Roles = "Administrator")]
        public ActionResult Index()
        {
            return View(scoreInterface.GetScoringMethodEditModel());
        }

        //
        // POST: /ScoringMethod/
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult Index(ScoringMethodEditModel scoringMethodEditModel)
        {
            if (ModelState.IsValid)
            {
                scoreInterface.AddScoringMethod(scoringMethodEditModel.NewScoringMethod);
                scoreInterface.Save();

                return RedirectToAction("Index");
            }

            ScoringMethod newScoringMethod = scoringMethodEditModel.NewScoringMethod;
            scoringMethodEditModel = scoreInterface.GetScoringMethodEditModel();
            scoringMethodEditModel.NewScoringMethod = newScoringMethod;
            return View(scoringMethodEditModel);
        }

        //
        // GET: /ScoringMethod/Edit/1

        [Authorize(Roles = "Administrator")]
        public ActionResult Edit(int id)
        {
            return View("Index", scoreInterface.GetScoringMethodEditModel(id));
        }

        // POST: /ScoringMethod/Edit/1
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult Edit(ScoringMethodEditModel scoringMethodEditModel)
        {
            if (ModelState.IsValid)
            {
                var updateScoringMethod = scoreInterface.GetScoringMethod(scoringMethodEditModel.NewScoringMethod.ScoringMethodID);
                updateScoringMethod.ScoringMethodName = scoringMethodEditModel.NewScoringMethod.ScoringMethodName;
                updateScoringMethod.HolePoints = scoringMethodEditModel.NewScoringMethod.HolePoints;
                updateScoringMethod.MatchPoints = scoringMethodEditModel.NewScoringMethod.MatchPoints;
                updateScoringMethod.BonusPoints = scoringMethodEditModel.NewScoringMethod.BonusPoints;
                updateScoringMethod.ScoringTypeID = scoringMethodEditModel.NewScoringMethod.ScoringTypeID;
                updateScoringMethod.ScoringCalculationID = scoringMethodEditModel.NewScoringMethod.ScoringCalculationID;

                if (TryUpdateModel(updateScoringMethod))
                {
                    scoreInterface.Save();
                    return RedirectToAction("Index");
                }
            }

            ScoringMethod newScoringMethod = scoringMethodEditModel.NewScoringMethod;
            scoringMethodEditModel = scoreInterface.GetScoringMethodEditModel();
            scoringMethodEditModel.NewScoringMethod = newScoringMethod;
            return View("Index", scoringMethodEditModel);
        }

        //
        // GET: /ScoringMethod/ScoringCalculations/

        [Authorize(Roles = "Administrator")]
        public ActionResult ScoringCalculations()
        {
            return View(new ScoringCalculationEditModel
            {
                NewScoringCalculation = new ScoringCalculation(),
                ScoringCalculations = scoreInterface.GetAllScoringCalculations(),
                ScoringVariables = scoreInterface.GetAllScoringVariables()
            });
        }

        //
        // POST: /ScoringMethod/ScoringCalculations/
        [HttpPost]
        [ValidateInput(false)]
        [Authorize(Roles = "Administrator")]
        public ActionResult ScoringCalculations(ScoringCalculationEditModel scoringCalculationEditModel)
        {
            if (ModelState.IsValid)
            {
                scoreInterface.AddScoringCalculation(scoringCalculationEditModel.NewScoringCalculation);
                scoreInterface.Save();
                scoringCalculationEditModel.NewScoringCalculation = new ScoringCalculation();
            }
            
            scoringCalculationEditModel.ScoringCalculations = scoreInterface.GetAllScoringCalculations();
            scoringCalculationEditModel.ScoringVariables = scoreInterface.GetAllScoringVariables();
            return View(scoringCalculationEditModel);
        }

        //
        // GET: /ScoringMethod/ScoringCalculationsEdit/5

        [Authorize(Roles = "Administrator")]
        public ActionResult ScoringCalculationsEdit(int id)
        {
            return View("ScoringCalculations", new ScoringCalculationEditModel
            {
                NewScoringCalculation = scoreInterface.GetScoringCalculation(id),
                ScoringCalculations = scoreInterface.GetAllScoringCalculations(),
                ScoringVariables = scoreInterface.GetAllScoringVariables()
            });
        }

        //
        // POST: /ScoringMethod/ScoringCalculationsEdit/5
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult ScoringCalculationsEdit(int id, ScoringCalculationEditModel scoringCalculationEditModel)
        {
            if (ModelState.IsValid)
            {
                ScoringCalculation updateScoringCalculation = scoreInterface.GetScoringCalculation(id);
                updateScoringCalculation.CalculationName = scoringCalculationEditModel.NewScoringCalculation.CalculationName;
                updateScoringCalculation.Formula = scoringCalculationEditModel.NewScoringCalculation.Formula;

                if (TryUpdateModel(updateScoringCalculation))
                {
                    scoreInterface.Save();
                    return RedirectToAction("ScoringCalculations");
                }
            }

            scoringCalculationEditModel.ScoringCalculations = scoreInterface.GetAllScoringCalculations();
            scoringCalculationEditModel.ScoringVariables = scoreInterface.GetAllScoringVariables();
            return View("ScoringCalculations", scoringCalculationEditModel);
        }

        //
        // POST: /ScoringMethod/ScoringCalculationsDelete/5
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult ScoringCalculationsDelete(int id)
        {
            ScoringCalculation deleteScoringCalculation = scoreInterface.GetScoringCalculation(id);
            scoreInterface.DeleteScoringCalculation(deleteScoringCalculation);
            scoreInterface.Save();

            return RedirectToAction("ScoringCalculations");
        }

        //
        // GET: /ScoringMethod/ScoringVariables/

        [Authorize(Roles = "Administrator")]
        public ActionResult ScoringVariables()
        {
            return View(new ScoringVariableEditModel
            {
                NewScoringVariable = new ScoringVariable(),
                ScoringVariables = scoreInterface.GetAllScoringVariables(),
            });
        }

        //
        // POST: /ScoringMethod/ScoringVariables/
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult ScoringVariables(ScoringVariableEditModel scoringVariableEditModel)
        {
            if (ModelState.IsValid)
            {
                scoreInterface.AddScoringVariable(scoringVariableEditModel.NewScoringVariable);
                scoreInterface.Save();
                return RedirectToAction("ScoringVariables");
            }
            else
            {
                scoringVariableEditModel.ScoringVariables = scoreInterface.GetAllScoringVariables();
                return View(scoringVariableEditModel);
            }
        }

        //
        // GET: /ScoringMethod/ScoringVariablesEdit/5

        [Authorize(Roles = "Administrator")]
        public ActionResult ScoringVariablesEdit(int id)
        {
            return View("ScoringVariables", new ScoringVariableEditModel
            {
                NewScoringVariable = scoreInterface.GetScoringVariable(id),
                ScoringVariables = scoreInterface.GetAllScoringVariables()
            });
        }

        //
        // POST: /ScoringMethod/ScoringVariablesEdit/5
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult ScoringVariablesEdit(int id, ScoringVariableEditModel scoringVariableEditModel)
        {
            if (ModelState.IsValid)
            {
                ScoringVariable updateScoringVariable = scoreInterface.GetScoringVariable(id);
                updateScoringVariable.Description = scoringVariableEditModel.NewScoringVariable.Description;
                updateScoringVariable.DisplayName = scoringVariableEditModel.NewScoringVariable.DisplayName;
                updateScoringVariable.ClassType = scoringVariableEditModel.NewScoringVariable.ClassType;

                if (TryUpdateModel(updateScoringVariable))
                {
                    scoreInterface.Save();
                    return RedirectToAction("ScoringVariables");
                }
                else
                {
                    scoringVariableEditModel.ScoringVariables = scoreInterface.GetAllScoringVariables();
                    return View("ScoringVariables", scoringVariableEditModel);
                }
            }
            else
            {
                scoringVariableEditModel.ScoringVariables = scoreInterface.GetAllScoringVariables();
                return View("ScoringVariables", scoringVariableEditModel);
            }
        }

        //
        // POST: /ScoringMethod/ScoringVariablesDelete/5
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult ScoringVariablesDelete(int id)
        {
            ScoringVariable deleteScoringVariable = scoreInterface.GetScoringVariable(id);
            scoreInterface.DeleteScoringVariable(deleteScoringVariable);
            scoreInterface.Save();

            return RedirectToAction("ScoringVariables");
        }
        
    }
}
